<%@page import="java.util.ArrayList"%>
<%@ page import = "num.WebManager" %>
<%@ page import = "org.ntu.csn.as.manager.DBManager" %>
<%@ page import = "org.ntu.csn.as.tool.Constant" %>
<%@ page import = "org.ntu.csn.as.model.Category" %>

<jsp:useBean id="n" class="num.WebManager" scope="session"/>
<jsp:useBean id="db" class="org.ntu.csn.as.manager.DBManager" scope="session"/>
<%!
    String nonull(String s) {
        return (s == null) ? "" : s;
    }

    String nospace(String s) {
        return s.replaceAll("\\s+", "");

    }
%>
<%
    System.out.println("difficulty level:"+request.getParameter("diffLevel"));
  
    //handle interfacing with back-end
    ArrayList<String> VarList = new ArrayList<String>();
    ArrayList<String> RelList = new ArrayList<String>();
    ArrayList<String> SolList = new ArrayList<String>();
    ArrayList<String> TplList = new ArrayList<String>();
    VarList = n.getVariableList();
    RelList = n.getRelationList();
    SolList = n.getSolutionList();
    TplList = n.getTemplateList();
    int var_count;
    String rsub[] = new String[30];
    String rs[] = new String[10];
    rs = db.getVarTypes(0);
    rsub = db.getVarTypes(9999);
    String qqtype=n.getQQtype();
    

    if (request.getParameter("submit") != null) {
        if (request.getParameter("submit").equals("Add New Type")) {
            String sPhrase = request.getParameter("phs");
            String sType1 = request.getParameter("type1");
            if (sPhrase != null && sType1 != null) {
                db.addVarType(sPhrase, sType1);
            }
        }
        if (request.getParameter("submit").equals("Delete Type")) {
            String typnum = request.getParameter("typenum");
            if (typnum != null) {
                db.DeleteType(typnum);
            }
        }
        if (request.getParameter("submit").equals("Auto Substitute")) {
            String sQuestion0 = request.getParameter("question");
            n.setNamQuestion(sQuestion0);
        }
        if (request.getParameter("submit").equals("Add to Dictionary")) {
            String sPhrase = request.getParameter("phs");
            String sPhrases = request.getParameter("phses");
            String sType1 = request.getParameter("type1");
            String sType2 = request.getParameter("type2");
            if (sPhrases.indexOf(".") != -1) {
                sPhrases = "";
            }
            if (sPhrase != null && sType1 != null) {
                db.addDictionary(sPhrase, sPhrases, sType1, sType2);
            }
        }
        if (request.getParameter("submit").equals("Add Template")) {
            String var_desc = request.getParameter("subtpl");
            if (var_desc != null) {
                n.setTemplate(var_desc);
                n.setCurr_temp(var_desc);
            }
        }
           if (request.getParameter("submit").equals("Auto Generate Variables")) {
            String sQuestion0 = request.getParameter("question");
            qqtype=request.getParameter("qqtype");
            n.setQQtype(qqtype);
            sQuestion0=n.AutoGenVar(sQuestion0);
            n.setQuestion0(sQuestion0, 0);
        }
        if (request.getParameter("submit").equals("Upd Question")) {
            String sQuestion0 = request.getParameter("question");
            qqtype=request.getParameter("qqtype");
            int diffLevel = 0;
            try{
             diffLevel = Integer.parseInt(request.getParameter("diffLevel"));
             }catch(Exception ex){
            	 diffLevel = 0;
             }
            n.setQQtype(qqtype);
            n.setQuestion0(sQuestion0, 1);
            n.setDiffLevel(diffLevel);
        }
        if (request.getParameter("submit").equals("Reset")) {
            n.setQuestion0(n.getDefaultQuestion(), 0);
            n.setQuestionId(-1);
        }

        if (request.getParameter("submit").equals("Update")) {
            String sVariable = request.getParameter("var");
            String sVar_type = request.getParameter("var_type");
            String sMin = request.getParameter("min");
            String sStep = request.getParameter("step");
            String sMax = request.getParameter("max");
            String sTimType = request.getParameter("timtype");
            sTimType = sTimType == null ? "isNumber" : sTimType;

            String sOrdType = request.getParameter("ordtype");
            sOrdType = sOrdType == null ? "isNumber" : sOrdType;

            String sFraType = request.getParameter("fratype");
            sFraType = sFraType == null ? "isNumber" : sFraType;

            //hj convert to integer if isFra
            if (sFraType.equals("isFra")) {
                sMin = String.valueOf((int) (Float.valueOf(sMin) * 100));
                sMax = String.valueOf((int) (Float.valueOf(sMax) * 100));
                sStep = String.valueOf((int) (Float.valueOf(sStep) * 100));
            }

            String var_desc = new String();
            var_desc = sVariable + ":" + sMin + ":" + sStep + ":" + sMax + ":" + sTimType + ":" + sOrdType + ":" + sFraType;
            if (sVariable != null) {
                n.setVariable(var_desc);
            }
        }
        if (request.getParameter("submit").equals("Add to Database")) {
            String[] varlist = new String[VarList.size()];
            VarList.toArray(varlist);
            String[] rellist = new String[RelList.size()];
            RelList.toArray(rellist);
            String[] sollist = new String[SolList.size()];
            SolList.toArray(sollist);
            String[] tpllist = new String[TplList.size()];
            TplList.toArray(tpllist);
            n.setQuestionId(db.addQuestion(n.getQuestion0(), qqtype,n.getDiffLevel(),varlist, rellist, sollist, tpllist));
         
        }
        if (request.getParameter("submit").equals("Update to Database")) {
            String[] varlist = new String[VarList.size()];
            VarList.toArray(varlist);
            String[] rellist = new String[RelList.size()];
            RelList.toArray(rellist);
            String[] sollist = new String[SolList.size()];
            SolList.toArray(sollist);
            String[] tpllist = new String[TplList.size()];
            TplList.toArray(tpllist);
            db.updateQuestion(String.valueOf(n.getQuestionId()), n.getQuestion0(), qqtype, varlist, rellist, sollist, tpllist);
        }
        if (request.getParameter("submit").equals("Add")) {
            String var_desc = request.getParameter("rel");
            if (var_desc != null) {
                n.setRelation(nospace(var_desc));
            }
        }
        if (request.getParameter("submit").equals("Delete")) {
            int i = Integer.valueOf(request.getParameter("rel_id"));
            RelList.remove(i);
            for (i = 0; i < RelList.size(); i++) {
                n.setRelation(RelList.get(i));
            }
        }
    }
    if (request.getParameter("submitsol") != null) {
        if (request.getParameter("submitsol").equals("Add")) {
            String var_desc = request.getParameter("solexpl");
            String var_desc2 = request.getParameter("solexpr");
            var_desc = var_desc + "var_sep" + var_desc2;
            if (var_desc != null) {
                n.setSolution(var_desc);
            }
        }
        if (request.getParameter("submitsol").equals("Delete")) {
            int i = Integer.valueOf(request.getParameter("sol_id"));
            SolList.remove(i);
            for (i = 0; i < SolList.size(); i++) {
                n.setSolution(SolList.get(i));
            }
        }
    }

    if (request.getParameter("submittpl") != null) {
        if (request.getParameter("submittpl").equals("Delete")) {
            int i = Integer.valueOf(request.getParameter("tpl_id"));
            TplList.remove(i);
            for (i = 0; i < TplList.size(); i++) {
                n.setTemplate(TplList.get(i));
            }
        }
    }
    if (request.getParameter("button") != null) {
        n.setHint("no hint");
        if (request.getParameter("button").equals("Delete")) {
            db.deleteQuestion(request.getParameter("myid"));
            n.setQuestion0(n.getDefaultQuestion(), 0);
            n.setQuestionId(0);
          
        }
        if (request.getParameter("button").equals("Edit")) {
            n.setCurr_temp(null);
            n.setQuestion0(db.getQuestion(request.getParameter("myid")));
            n.setQuestionId(Integer.valueOf(request.getParameter("myid")));
            qqtype=db.getQuestionTyp(request.getParameter("myid"));
            n.setQQtype(qqtype);
            VarList = db.getDefinition(request.getParameter("myid"));
            for (int i = 0; i < VarList.size(); i++) {
                n.setVariable(VarList.get(i));
            }
            RelList = db.getRelation(request.getParameter("myid"));
            for (int i = 0; i < RelList.size(); i++) {
                n.setRelation(RelList.get(i));
            }
            SolList = db.getSolution(request.getParameter("myid"));
            for (int i = 0; i < SolList.size(); i++) {
                n.setSolution(SolList.get(i));
            }
            TplList = db.getTemplate(request.getParameter("myid"));
            for (int i = 0; i < TplList.size(); i++) {
                n.setTemplate(TplList.get(i));
            }
        }
    }
%>
<table width="100%" border="0">
    <tr>
        <td width="65%">  
            <div>
                <form action="QuestionMan.jsp" method=post> 
                    <% if (n.getQuestionId() > 0) {%>
                    <b>Editing Question NO. <%=n.getQuestionId()%>, You can </b>                        
                            <input name="submit" type=submit value="Update to Database">                          
                            <input name="submit" type=submit value="Add to Database"> 
                            <%} else {%>
                            <b>Creating new Template, Remember to</b>
                                    <input name="submit" type=submit value="Add to Database"> 
                                    <% }%>
                                    </form>
                                    </div>
                                    <div id="MesBoard">Topic: <%=qqtype%></div>
                                    </td> 
                                    </tr>

                                    </table>